﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MovieRental_Presentation.DataViews;
using MovieRental.EnhancedEntities;
using MovieRental_Presentation.Helpers;

namespace MovieRental_Presentation
{
	public partial class Movies
	{
        private MoviesDataView _dataView;

		public Movies()
		{
			this.InitializeComponent();
            _dataView = new MoviesDataView();
            DataContext = _dataView;

			// Insert code required on object creation below this point.
		}

		private void dashboardButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.NavigationService.Navigate(new Dashboard());
		}

		private void editButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.NavigationService.Navigate(new MovieDetails((Movie) moviesDataGrid.SelectedItem, false));
			// TODO: Add event handler implementation here.
		}

		private void addButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// TODO: Add event handler implementation here.
			this.NavigationService.Navigate(new NewMovieDialog());
		}

        private void searchBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Space)
                _dataView.SearchString = searchBox.Text;
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void moviesDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (moviesDataGrid.SelectedItem != null)
                NavigationService.Navigate(new MovieDetails((Movie) moviesDataGrid.SelectedItem));
        }

        private void customersButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Customers());
        }

        private void statisticsButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Statistics());
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            Movie movie = moviesDataGrid.SelectedItem as Movie;
            var globalResources = (Application.Current as App).Resources;
            if (!ComponentManager.Instance.MovieComponent.CanDelete(movie.Entity))
            {
                string message = string.Format((string)globalResources["deleteMovieFailedMessage"], movie.Entity.Title, movie.NotReturnedLoansCount);
                MessageBox.Show(message, "Can't delete movie.", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            var result = MessageBox.Show("Do you really want do delete this movie?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
                ComponentManager.Instance.MovieComponent.Delete(movie.Entity);
        }
		
	}
}